// 这里是写vuex的getter

export default {
    doneOpenId: state => {
        state.openId = localStorage.getItem('openId');
        return state.openId
    },
    doneUserId: state => {
        state.userId = localStorage.getItem('userId');
        return state.userId
    },
    doneSchoolId: state => {
        state.schoolId = localStorage.getItem('schoolId');
        return state.schoolId
    },
    donePhone: state => {
        state.phone = localStorage.getItem('phone');
        return state.phone
    },
    doneAvatar: state => {
        state.avatar = localStorage.getItem('avatar');
        return state.avatar
    },
    donePassword: state => {
        state.password = localStorage.getItem('password');
        return state.password
    },
    doneSchoolCode: state => {
        state.schoolCode = localStorage.getItem('schoolCode');
        return state.schoolCode
    },
    // 店铺是否全选
    isAllSelectByDian: state => dianId => {
        return state
            .carts[dianId]
            .shops
            .every(shop => {
                return shop.select
            })
    },
    // 所有店铺是否全选
    isAllSelect: (state, { isAllSelectByDian }) => {
        return state
            .carts
            .every((cart, dianId) => {
                return isAllSelectByDian(dianId)
            })
    },
    // 计算单个商品价格
    subTotal: state => (dianId, shopIndex) => {
        const shop = state.carts[dianId].shops[shopIndex]
        return shop.count * shop.price
    },
    // 店铺小计
    dianTotal: ({
        carts
    }, { subTotal }) => dianId => {
        let price = 0
        let shops = carts[dianId].shops
        for (let shopIndex = 0; shopIndex < shops.length; shopIndex++) {
            const shop = shops[shopIndex]
            price += shop.select ?
                subTotal(dianId, shopIndex) :
                0
        }
        return Math.floor(price*100)/100
    },
    // 合计
    total: ({
        carts   //取state.carts
    }, { dianTotal }) => {
        return carts.reduce((val, dian, dianId) => {
            let totals=val +Number(dianTotal(dianId))
            carts[dianId].diantotal=dianTotal(dianId)
            let bb=Math.floor(totals*100)/100
            localStorage.setItem("total",bb.toString()) //本地进行存储
            return totals
        }, 0)
    },
    //总价格获取
    getTatal:state=>{
       let totals=JSON.parse(localStorage.getItem('total'));
       return totals
    },

    //购物车赋值
    carts: state => state.carts,
    getLocalorder:state =>{     
        state.order= JSON.parse(localStorage.getItem('order'));  //也让面刷新state负值
        return state.order
    }
}